<%--
  Created by IntelliJ IDEA.
  User: MSI
  Date: 2022/5/7
  Time: 10:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">

        <form class="layui-form">
            <div class="layui-form-item">
                <label  class="layui-form-label">
                    <span class="x-red">*</span>员工姓名
                </label>
                <div class="layui-input-inline">
                    <select id="userName"></select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>

            <div class="layui-form-item">
                <label  class="layui-form-label">
                    <span class="x-red">*</span>分配角色
                </label>
                <div class="layui-input-inline">
                    <select name="main_id" id="main_id">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>

        <div class="layui-form-item">
            <button  onclick="add()">
                增加
            </button>
        </div>
    </form>
</div>
<script>
    function add(){
        var userId = $("#userName").find("option:selected").val();
        var roleId = $("#main_id").find("option:selected").val();
        $.ajax({
            url: '/userRoleAjax/add',
            dataType: 'json',
            type: 'post',
            contentType: "application/x-www-form-urlencoded",
            success: function (rs) {
                console.log(rs);
                layer.alert(rs.info, {icon: 6}, function () {
                        if (rs.success) {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            //刷新
                            parent.window.loadData();
                        }
                    }
                )
            }
        });
    }
    function loadRoleName(){
        $.ajax({
            url: '/roleAjax/list',
            dataType: 'json',
            type: 'post',
            contentType:"application/x-www-form-urlencoded",
            async:false,
            success: function (rs) {
                console.log(rs.data[0].roleName);
                for (var i = 0; i < rs.data.length; i++) {
                    $("#main_id").append("<option value='"+rs.data[i].roleId+"' >" + rs.data[i].roleName + "</option>");
                }
            }
        });
    }
    function loadName(){
        $.ajax({
            url: '/userAjax/queryNoRole',
            dataType: 'json',
            type: 'post',
            contentType:"application/x-www-form-urlencoded",
            async:false,
            success: function (rs) {
                console.log(rs);
                console.log(rs.data[0].name);
                for (var i = 0; i < rs.data.length; i++) {
                    $("#userName").append("<option value='"+rs.data[i].userId+"' >" + rs.data[i].name + "</option>");
                }
            }
        });
    }
        // //监听提交
        // form.on('submit(add)', function(data){
        //     $.ajax({
        //         url:"/userAjax/add",
        //         type:"post",
        //         contentType:"application/x-www-form-urlencoded",
        //         data:data.field,
        //         dataType:"json",
        //         success:function (rs){
        //             console.log(rs);
        //             //发异步，把数据提交给php
        //             layer.alert(rs.info, {icon: 6},function () {
        //                 if (rs.success){
        //                     // 获得frame索引
        //                     var index = parent.layer.getFrameIndex(window.name);
        //                     //关闭当前frame
        //                     parent.layer.close(index);
        //                     //刷新
        //                     parent.window.loadData();
        //                 }
        //             });
        //         }
        //     })
    // });
    $(function () {
        //运行加载数据
        loadName();
        loadRoleName();
    })
</script>
<script>var _hmt = _hmt || []; (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>